<admintpl file="header" />
<style type="text/css">
.pic-list li {
	margin-bottom: 5px;
}
</style>
<script type="text/html" id="photos-item-wrapper">
	<li id="savedimage{id}">
		<input id="photo-{id}" type="hidden" name="photos_url[]" value="{filepath}"> 
		<input id="photo-{id}-name" type="text" name="photos_alt[]" value="{name}" style="width: 160px;" title="图片名称">
		<img id="photo-{id}-preview" src="{url}" style="height:36px;width: 36px;" onclick="parent.image_preview_dialog(this.src);">
		<a href="javascript:upload_one_image('图片上传','#photo-{id}');">替换</a>
		<a href="javascript:(function(){$('#savedimage{id}').remove();})();">移除</a>
	</li>
</script>
</head>
<body>
	<div class="wrap js-check-wrap">
		<ul class="nav nav-tabs">
			<li><a href="{:U('Questions/index')}">考题管理</a></li>
			<li class="active"><a href="{:U('Questions/add',array('term'=>empty($term['term_id'])?'':$term['term_id']))}" target="_self">添加考题</a></li>
			<li><a href="{:U('Questions/import')}" target="_self">批量导入</a></li>
			<li><a href="{:U('Questions/recyclebin',array('term'=>empty($term['term_id'])?'':$term['term_id']))}" target="_self">试题回收站</a></li>
		</ul>
		<form action="{:U('Questions/add_question')}" method="post" class="form-horizontal js-ajax-forms">
			<div class="row-fluid">
				<div class="span9">
					<table class="table table-bordered">
						<tr>
							<th width="80">试题分类</th>
							<td>
								<select style="max-height: 100px;" name="ques_term" id="ques_term">{$taxonomys}</select>
							</td>
						</tr>
						<tr>
							<th width="80">试题类型</th>
							<td>
								<select style="max-height: 100px;" name="ques_type" id="ques_type" required onchange="showlist()">
								<option value="">选择试题类型</option>
								<foreach name="question_types" item="vo">
						        	<option value="{$key}">{$vo}</option>
								</foreach>
								</select>
							</td>
						</tr>
						<tr id="question_option_count">
							<th width="80">选项数目</th>
							<td>
								<select style="max-height: 100px;" name="option_count" id="option_count" onchange="optionList(this);">
								<option>2</option>
								<option>3</option>
								<option>4</option>
								<option>5</option>
								<option>6</option>
								</select>
							</td>
						</tr>
						<tr>
							<th width="80">试题状态</th>
							<td>
								<select style="max-height: 100px;" name="status" id="status">
								<option value="0">启用</option>
								<option value="1">停用</option>
								</select>
							</td>
						</tr>
						<tr>
							<th width="80">试题难度</th>
							<td>
								<select style="max-height: 100px;" name="ques_level" id="ques_level">
								<foreach name="question_level" item="vo">								
								<option value="{$key}">{$vo}</option>
								</foreach>
								</select>
							</td>
						</tr>
						<tr>
							<th>默认分数</th>
							<td>
								<input type="text" style="width:200px;" name="ques_score" id="ques_score" required value="" placeholder="请输入分数"/>分
								，默认分数用于日常练习积分使用
							</td>
						</tr>
						<tr>
							<th>排序</th>
							<td><input type="text" name="listorder" id="listorder" value="" style="width: 200px" placeholder="选填">值越小越先前 </td>
						</tr>
						<tr>
							<th>题干</th>
							<td>
								<script type="text/plain" id="ques_content" name="ques_content"></script>
							</td>
						</tr>
						<tr id="question_options" style="display:none">
							<th>答案列表</th>
							<td>
							<div  class="lineD" style="display:none" id="single_option"><ol type="A"></ol></dd>
							</div>
							<div   class="lineD" style="display:none" id="many_option"><ol type="A"></ol>
							</div>
							
							请勾选正确答案。
							</td>
						</tr>
						
						<tr id="empty_options" style="display:none">
							<th>填空题答案</th>
							<td>
							<div   class="lineD" id="empty_option"><ol></ol>
							</div>							
							请选择答案数量后，在此填写正确答案。
							</td>
						</tr>
						<tr>
							<th>答案解析</th>
							<td>
								<textarea name="ques_guide" id="ques_guide" style="width: 98%; height: 50px;" placeholder="请填写摘要"></textarea>
							</td>
						</tr>
						<tr>
							<th>考题来源</th>
							<td><input type="text" name="ques_source" id="ques_source" value="" style="width: 400px" placeholder="请输入文章来源"></td>
						</tr>
					</table>
				</div>
			</div>
			<div class="form-actions">
				<button class="btn btn-primary js-ajax-submit" type="submit">提交</button>
				<a class="btn" href="{:U('Questions/index')}">返回</a>
			</div>
		</form>
	</div>
	<script type="text/javascript" src="__PUBLIC__/js/common.js"></script>
	<script type="text/javascript">
		//编辑器路径定义
		var editorURL = GV.WEB_ROOT;
	</script>
	<script type="text/javascript" src="__PUBLIC__/js/ueditor/ueditor.frontend.config.js"></script>
	<script type="text/javascript" src="__PUBLIC__/js/ueditor/ueditor.all.min.js"></script>
	<script type="text/javascript">
	var single=document.getElementById('single_option');
	var many=document.getElementById('many_option');
	var empty=document.getElementById('empty_option');
	var olBox=single.getElementsByTagName('ol')[0];
	var emBox=many.getElementsByTagName('ol')[0];
	var eyBox=empty.getElementsByTagName('ol')[0];
	var selNum=document.getElementById('option_count').value;
	var qid = 0;
	if( qid == 0 ) {
		for(var i=0;i<selNum;i++){
			olBox.innerHTML+='<li><input type="radio" name="opt" id="opt'+(i+1)+'" value="'+(i+1)+'"><input style="width:300px;" type="text" name="single_question_answer[]" class="text0'+(i+1)+'"></li>';
			emBox.innerHTML+='<li><input type="checkbox" name="opts[]" id="opt'+(i+1)+'" value="'+(i+1)+'"><input style="width:300px;" type="text" name="many_question_answer[]" class="text0'+(i+1)+'"></li>';
			eyBox.innerHTML+='<li><input style="width:300px;" type="text" id="opt'+(i+1)+'" name="empty_question_answer[]" class="text0'+(i+1)+'"></li>';
		}
	}
	function optionList(element){
		var selNum=element.value;
		olBox.innerHTML='';	
		emBox.innerHTML='';
		eyBox.innerHTML='';
		for(var i=0;i<selNum;i++){
			olBox.innerHTML+='<li><input type="radio" name="opt" value="'+(i+1)+'"><input style="width:300px;" type="text" name="single_question_answer[]" class="text0'+(i+1)+'"></li>';
			emBox.innerHTML+='<li><input type="checkbox" name="opts[]" value="'+(i+1)+'"><input style="width:300px;" type="text" name="many_question_answer[]" class="text0'+(i+1)+'"></li>';
			eyBox.innerHTML+='<li><input style="width:300px;" type="text" name="empty_question_answer[]" class="text0'+(i+1)+'"></li>';
		}
	}
	var ans=document.getElementById('box_answer');
	function removeLi(elem){
		var elementLi=elem.parentNode;				//获取当前a元素的父节点
		ans.removeChild(elementLi);				    //删除这个li节点
		var val = $(elem).data('num');
		var str = E.getData();
		if( str.indexOf(val) != -1 ) {
			E.setData(str.replace('<img class="tkimg" alt="'+val+'" />' , ''));
		}
	}

	function showlist(){
        var o = document.getElementById('ques_type');
        var strValue = o.options[o.options.selectedIndex].value;
        if(strValue == "1" || strValue == "4") {
            document.getElementById('question_option_count').style.display = "";
            document.getElementById('single_option').style.display = "";

            document.getElementById('question_options').style.display = "";
            document.getElementById('many_option').style.display = "none";
            document.getElementById('empty_options').style.display = "none";
            if( strValue == "4" ) {
            	document.getElementById('option_count').disabled = true;
            	document.getElementById('option_count').value=2;
            	document.getElementById('option_count').onchange();
            }
        }
        else if(strValue == "2") {
            document.getElementById('question_option_count').style.display = "";
            document.getElementById('question_options').style.display = "";
            document.getElementById('single_option').style.display = "none";
            document.getElementById('many_option').style.display = "";
            document.getElementById('empty_options').style.display = "none";
            document.getElementById('option_count').disabled = false;
        }
        else if(strValue == "3") {
            document.getElementById('question_option_count').style.display = "";
            document.getElementById('question_options').style.display = "none";
            document.getElementById('single_option').style.display = "none";
            document.getElementById('many_option').style.display = "none";
            document.getElementById('empty_options').style.display = "";
            document.getElementById('option_count').disabled = false;
        }
		else if (strValue == "0") {
			document.getElementById('question_option_count').style.display = "none";
            document.getElementById('single_option').style.display = "none";
            document.getElementById('many_option').style.display = "none";
            document.getElementById('question_options').style.display = "none";
            document.getElementById('empty_options').style.display = "none";
		}
		else {
            document.getElementById('question_option_count').style.display = "none";
            document.getElementById('single_option').style.display = "none";
            document.getElementById('many_option').style.display = "none";
            document.getElementById('question_options').style.display = "none";
            document.getElementById('empty_options').style.display = "none";
        }
    }

	
$(function() {
	$('#ques_type').val(1);
	$('#ques_type').change();
	//alert(UE.getEditor('editor').getContent());
	$(".js-ajax-close-btn").on('click', function(e) {
		e.preventDefault();
		Wind.use("artDialog", function() {
			art.dialog({
				id : "question",
				icon : "question",
				fixed : true,
				lock : true,
				background : "#CCCCCC",
				opacity : 0,
				content : "您确定需要关闭当前页面嘛？",
				ok : function() {
					setCookie("refersh_time", 1);
					window.close();
					return true;
				}
			});
		});
	});
	/////---------------------
	Wind.use('validate', 'ajaxForm', 'artDialog', function() {
		//javascript
		//编辑器
		var editor = UE.getEditor('ques_content');
		//增加编辑器验证规则
		jQuery.validator.addMethod('editorcontent', function() {
			return editorcontent.hasContents();
		});
		jQuery.validator.addMethod('quesoptions', function() {
			if($('#ques_type').val()==1||$('#ques_type').val()==4){				
				$('#single_option').find(":text").each(function(){
					if($(this).val().length==0)return false;
				});
			}
			else if($('#ques_type').val()==2||$('#ques_type').val()==3){
				$('#many_option').find(":text").each(function(){
					if($(this).val().length==0)return false;
				});
			}
			return true;
		});
		var form = $('form.js-ajax-forms');
		//ie处理placeholder提交问题
		if ($.browser && $.browser.msie) {
			form.find('[placeholder]').each(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			});
		}
		var formloading = false;
		//表单验证开始
		form.validate({
			//是否在获取焦点时验证
			onfocusout : false,
			//是否在敲击键盘时验证
			onkeyup : false,
			//当鼠标掉级时验证
			onclick : false,
			//验证错误
			showErrors : function(errorMap, errorArr) {
				//errorMap {'name':'错误信息'}
				//errorArr [{'message':'错误信息',element:({})}]
				try {
					$(errorArr[0].element).focus();
					art.dialog({
						id : 'error',
						icon : 'error',
						lock : true,
						fixed : true,
						background : "#CCCCCC",
						opacity : 0,
						content : errorArr[0].message,
						cancelVal : '确定',
						cancel : function() {
							$(errorArr[0].element).focus();
						}
					});
				} catch (err) {
				}
			},
			//验证规则
			rules : {
				ques_term : {
					required : true
				},
				ques_type : {
					required : true
				},
				ques_score : {
					required : true
				},
				ques_content : {
					required : true,
					editorcontent : true
				},
// 				opt : {
// 					required : $('#ques_type').val()==1||$('#ques_type').val()==4
// 				},
// 				'opts[]' : {
// 					required : $('#ques_type').val()==2
// 				},
				
			},
			//验证未通过提示消息
			messages : {
				ques_term : {
					required : '请选择考题分类'
				},
				ques_type : {
					required : '请选择题型'
				},
				ques_score : {
					required : '请填写试题分数'
				},
				ques_content : {
					required : '请填写题干',
					editorcontent : '内容不能为空'
				}			
				
			},
// 			errorPlacement: function (error, element) { //指定错误信息位置
// 				if (element.is(':radio') || element.is(':checkbox')) {
// 					var eid = element.attr('name');
// 					error.appendTo(element.parent());
// 				} else {
// 					error.insertAfter(element);
// 				}
// 			},
			//给未通过验证的元素加效果,闪烁等
			highlight : false,
			//是否在获取焦点时验证
			onfocusout : false,
			//验证通过，提交表单
			submitHandler : function(forms) {
				if (formloading)
					return;
				$(forms).ajaxSubmit({
					url : form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
					dataType : 'json',
					beforeSubmit : function(arr, $form, options) {
						formloading = true;
					},
					success : function(data, statusText, xhr, $form) {
						formloading = false;
						if (data.status) {
							setCookie("refersh_time", 1);
							//添加成功
							Wind.use("artDialog", function() {
								art.dialog({
									id : "succeed",
									icon : "succeed",
									fixed : true,
									lock : true,
									background : "#CCCCCC",
									opacity : 0,
									content : data.info,
									button : [ {
										name : '继续添加？',
										callback : function() {
											reloadPage(window);
											return true;
										},
										focus : true
									}, {
										name : '返回列表页',
										callback : function() {
											location = "{:U('Questions/index')}";
											return true;
										}
									} ]
								});
							});
						} else {
							artdialog_alert(data.info);
						}
					}
				});
			}
		});
		$('#ques_type').change(function(){
			if($(this).val()==4){
				$('#single_option').find(":text").each(function(index,element){
					if(index==0)
						$(this).val('正确');
					else
						$(this).val('不正确');
				});
			}
			else{
				$('#single_option').find(":text").each(function(index,element){
					$(this).val('');
				});
			}
		});
// 		$('#ques_type').change(function(){
// 			if($(this).val()==1){
// 				$("input[name='opt']").rules("add", { required: true, messages: { required: "请选择单选题答案"} });
// 				$("input[name='single_question_answer']").rules("add", { required : true, messages: { required: "请填写选项内容"} });
// 				$("input[name='single_question_answer']").rules("add", { quesoptions : true, messages: { required: "请填写单选题选项内容"} });
// 			}
// 			else if($(this).val()==2){
// 				$("input[name='opts[]']").rules("add", { required: true, messages: { required: "请选择多选题答案"} });
// 				$("input[name='many_question_answer']").rules("add", { quesoptions : true, messages: { required: "请填写多选题选项内容"} });
// 			}
// 			else if($(this).val()==3){
// 				$("input[name='opt']").rules("add", { required: true, messages: { required: "请选择填空题答案"} });
// 				$("input[name='empty_question_answer']").rules("add", { quesoptions : true, messages: { required: "请选择填空题选项内容"} });
// 			}
// 			else if($(this).val()==4){
// 				$("input[name='opt']").rules("add", { required: true, messages: { required: "请选择判断题答案"} });
// 				$("input[name='single_question_answer']").rules("add", { quesoptions : true, messages: { required: "请选择判断题选项内容"} });
// 			}
// 		});
	});
	////-------------------------
});
	</script>
</body>
</html>